package com.jy.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.commonutils.R;
import com.jy.eduservice.entity.EduTeacher;
import com.jy.eduservice.entity.vo.TeacherQuery;
import com.jy.eduservice.service.EduTeacherService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-10
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    //1.查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }
    @ApiOperation(value = "根据ID删除讲师")
    //2.逻辑删除讲师的方法
    //浏览器只是测 get,post,put,提交 ,浏览器不能测试delete提交 , 如何测试?
    //用工具测试 swagger  ,postman
    //http://localhost:8001/swagger-ui.html

    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //3.分页查询讲师的方法 , current 当前页 ; limit 每页记录数
//

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageList(@PathVariable long current,@PathVariable long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法的时候,底层封装,把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, null);
        //总记录数
        long total = pageTeacher.getTotal();
        //每页数据 ,list集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }
    //4.条件查询讲师分页的方法
    //@RequestBody  需要使用post提交方式 ; 使用json传递数据,把json数据封装到对应对象里面
                    //(required = false) 表示里面TeacherQuery的值可以没有
    //@ResponseBody  返回json数据
    @PostMapping ("pageTeacherCondition/{current}/{limit}")
    @ApiOperation(value = "条件查询讲师分页列表")
    public R pageTeacherCondition(@PathVariable long current , @PathVariable long limit ,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构造条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空,不为空则拼接条件
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        //调用方法实现条件查询分页 ,把数据存入pageTeacher中
        eduTeacherService.page(pageTeacher,queryWrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    //5.添加讲师
    @ApiOperation(value = "新增讲师")
    @PostMapping("/addTeacher")
    //测试的时候,时间需要去掉,因为已经有了自动填充,id也需要去掉
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //6.根据ID查询讲师
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getById(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

     /* 7.根据id修改讲师
        修改功能: 1.先根据讲师id进行查询,进行数据的回显
                 2. 在进行讲师修改    */
    //测试时时间不用写,因为有自动填充, id要传
    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher/{id}")
    public R updateById(@PathVariable String id,@RequestBody EduTeacher eduTeacher){
        eduTeacher.setId(id);
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

